﻿using COFE.Core;
using COFE.Core.Lister;
using COFE.Core.Profile;
using COFE.Core.Entry;

namespace COFE.Data
{
    /// <summary>
    /// In addition to construct dbEntry, it write an dbEntry (not it's properties) to database and attach a cache property provider.
    /// </summary>
    public class DBEntryConstructor : IEntryConstructor
    {
        #region Constructor

        #endregion

        #region Methods

        public static IFileSystemInfo CreateEntry(Entry dbEntry, EntryConstructionFlags flags)
        {
            return COFEShared.EntryConstructor.CreateEntry(dbEntry.SecondaryVolumeId,
                    new DBCachePropertyProvider(dbEntry), flags);
        }

        public IInternalFileInfoExA CreateFile(string listerKey, IPropertyProvider propProvider,
            EntryConstructionFlags flags = EntryConstructionFlags.Default)
        {
            if (propProvider is IPropertyProviderCache) //If the sending propertyProvider is cached, do nothing.
            {
                var cachePP = propProvider as IPropertyProviderCache;
                return new FileInfoExA(listerKey, cachePP);
            }
            else
                if (!flags.HasFlag(EntryConstructionFlags.EnableCache)) //Cache disabled, do nothing.
                {
                    return new FileInfoExA(listerKey, propProvider);
                }
                else
                {
                    var dbEntry = COFEDBShared.DBEntryManager.UpdateEntry(propProvider);
                    return new FileInfoExA(listerKey, new DBCachePropertyProvider(dbEntry));                    
                }
        }

        public IInternalDirectoryInfoExA CreateDirectory(string listerKey, IPropertyProvider propProvider,
            EntryConstructionFlags flags = EntryConstructionFlags.Default)
        {
            if (propProvider is IPropertyProviderCache) //If the sending propertyProvider is cached, do nothing.
            {
                var cachePP = propProvider as IPropertyProviderCache;
                return new DirectoryInfoExA(listerKey, cachePP);
            }
            else
            {
                var cofeAttribute = (COFEAttributes)propProvider.GetProperty(FileSystemInfoProperties.COFEAttributes).Value;

                if (cofeAttribute.HasFlag(COFEAttributes.NotCachable) || 
                    cofeAttribute.HasFlag(COFEAttributes.AlreadyCached) ||
                    !flags.HasFlag(EntryConstructionFlags.EnableCache)) 
                    //Cache disabled, do nothing.
                {
                    return new DirectoryInfoExA(listerKey, propProvider);
                }
                else
                {
                    var dbEntry = COFEDBShared.DBEntryManager.UpdateEntry(propProvider);
                    return new DirectoryInfoExA(listerKey, new DBCachePropertyProvider(dbEntry));                    
                }
            }
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        #endregion

    }
}
